﻿using System.Collections.Generic;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Interfaces.Movie;
using Rodunu.Movies.Interfaces.User;
using Rodunu.Movies.Logic.Movie;
using Rodunu.Movies.Logic.User;

namespace Rodunu.Movies.Web.Controllers
{
    internal enum PageTypes
    {
        AddMovie,
        MovieDetail,
        Movies,
        Freinds,
        Profile,
        Home
    }

    public class UserBaseController : BaseController
    {
        protected IList<FriendMovieRequest> _friendMovieRequests;
        protected IList<Friends> _friendsRequests;

        protected void GetFriendMovieRequests()
        {
            IFriendMovieRequest friendMovieRequestLogic = new FriendMovieRequestLogic();
            _friendMovieRequests = friendMovieRequestLogic.GetPendingMovieRequestsForUser(User.Id);
            PropertyBag["FriendMovieRequests"] = _friendMovieRequests;
        }

        protected void FriendsRequests()
        {
            IFriends friendsLogic = new FriendsLogic();
            _friendsRequests = friendsLogic.GetPendingFreindsRequestsForUser(User.Id);
            PropertyBag["PendingFriendsRequests"] = _friendsRequests;
        }

        private int SetRequests()
        {
            GetFriendMovieRequests();
            FriendsRequests();
            return _friendMovieRequests.Count +_friendsRequests.Count;
        }

        private void SetTopToolBar()
        {
            IUserMovies userMoviesLogic = new UserMoviesLogic();
            PropertyBag["OnLoanHeader"] = userMoviesLogic.GetMoviesByUSerAdvanced(User.Id, null, null, null, 2, null).Count;
            PropertyBag["TotalMovies"] = userMoviesLogic.GetTotalMovies(User.Id);
            PropertyBag["Collections"] = userMoviesLogic.GetCollectionsForUser(User.Id);
            PropertyBag["Discs"] = userMoviesLogic.GetDiscs(User.Id);
            PropertyBag["Borrowed"] = userMoviesLogic.GetBorrowed(User.Id);
        }

        internal void SetLayout(bool redirect)
        {
            if (User != null && User.IsLoggedIn)
            {
                IUser userLogic = new UserLogic();
                UserStatusTypes.Status status = userLogic.GetUserStatus(User.Id);
                if (status == UserStatusTypes.Status.Active)
                {
                    LayoutName = "default";
                    PropertyBag["RequestsCount"] = SetRequests();
                    SetTopToolBar();
                }
                else
                {
                    Redirect("Authentication", "Index");
                }
            }
            else
            {
                //if (redirect)
                //    RenderView("Home", "Index");
                //else
                //    Redirect("Authentication", "Index");
                Redirect("Authentication", "Index");
            }

            PropertyBag["user"] = User;
        }

        internal void SetPage(PageTypes pageType)
        {
            PropertyBag["MoviesClass"] = "notactive";
            PropertyBag["FriendsClass"] = "notactive";
            PropertyBag["ProfileClass"] = "notactive";
            PropertyBag["HomeClass"] = "notactive";
            switch (pageType)
            {
                case PageTypes.AddMovie:
                    break;
                case PageTypes.Freinds:
                    PropertyBag["FriendsClass"] = "active";
                    break;
                case PageTypes.MovieDetail:
                    break;
                case PageTypes.Movies:
                    PropertyBag["MoviesClass"] = "active";
                    break;
                case PageTypes.Profile:
                    PropertyBag["ProfileClass"] = "active";
                    break;
                default:
                    PropertyBag["HomeClass"] = "active";
                    break;
            }
        }
    }
}
